package com.qf.cx.simplenms.persist.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

import com.qf.cx.simplenms.persist.dao.api.SwitchDao;
import com.qf.cx.simplenms.persist.entity.SwitchEntity;

@Repository
public class SwitchDaoImpl implements SwitchDao {
	@Autowired
	@Qualifier("hibernateSessionFactory")
	private SessionFactory sessionFactory;

	@Override
	public void addSwitch(SwitchEntity switch0) {
		Session session = sessionFactory.getCurrentSession();
		session.save(switch0);
	}

	@Override
	public void deleteSwitch(SwitchEntity switch0) {
		Session session = sessionFactory.getCurrentSession();
		session.delete(switch0);
	}

	@Override
	public SwitchEntity getOneSwitch() {
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("from SwitchEntity");
		query.setMaxResults(1);
		List<SwitchEntity> list = query.list();
		SwitchEntity r = null;
		if (!list.isEmpty()) {
			r = list.get(0);
		}
		return r;
	}

	@Override
	public void deleteAll() {
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("delete from SwitchEntity");
		query.executeUpdate();
	}

}
